package Dane;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import jmp3player.Jmp3PlayerView;

/**
 * Klasa przechowująca opcje programu
 * @author lukeh
 */
public class Settings {
    /**
     * Wektor przechowujący adresy folderów do przeszukania
     */
    public Vector<String> foldery;
/**
 * Konstruktor inicjujący foldery
 */
    public Settings()
    {
        foldery = new Vector<String>();
    }
/**
 * Zmienia klasę na XML
 * @return tresć XML
 */
public String settings2Xml() {
                XStream mapping=new XStream(new DomDriver());
                String xml=mapping.toXML(this);
                return xml;
            }
/**
 * Uzupełnia klase wartościami z pliku XML
 * @param xml - treść pliku XML
 */
public void xml2settings(String xml){
                XStream mapping=new XStream(new DomDriver());
                this.foldery = ((Settings) mapping.fromXML(xml)).foldery;
                
            }
/**
 * Dodaje adres folderu do listy
 * @param addr - adres folderu
 */
public void addDir(String addr)
{
    foldery.add(addr);
}
/**
 * Pobiera stan klasy opcji z pliku
 */
public void getSetFile()
 {
     File settingsFile = new File("settings.xml");
        if (settingsFile==null)
        {
            try {
                settingsFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(Jmp3PlayerView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        Settings set = new Settings();
        FileInputStream fs;
        try {
            fs = new FileInputStream("settings.xml");
            DataInputStream ds = new  DataInputStream(fs);

            String s= new String(), s1 = new String();
            while(true){
                s1 = ds.readLine();
                if(s1==null) break;
                s+=s1+"\n";

            }

           set.xml2settings(s);
           this.foldery = set.foldery;
        } catch (Exception ex) {
            Logger.getLogger(Jmp3PlayerView.class.getName()).log(Level.SEVERE, null, ex);
        }
 }
/**
 * Zapisuje stan klasy opcji do pliku
 */
public void saveSetFile()
{
    String s = settings2Xml();
    FileOutputStream fs;
        try {
            fs = new FileOutputStream("settings.xml");
            DataOutputStream dos = new DataOutputStream(fs);
            try {
                dos.write(s.getBytes());
            } catch (IOException ex) {
                Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
        }
}

}
